<?php
require_once("database.php");
class xl_san_pham extends database
{
	function danh_sach_theo_loai($ma_loai_san_pham, $vi_tri, $so_luong)
	{
		$sql = "select * from san_pham where ma_loai_san_pham = '$ma_loai_san_pham' limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_tim_kiem($key, $vi_tri, $so_luong)
	{
		$sql = "select * from san_pham where ten like '%$key%' limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_ban_chay()
	{
		$sql = "SELECT sp.*, ct.ma_san_pham, sum(ct.so_luong) tong_so_luong
				FROM san_pham sp, chi_tiet_hoa_don ct
				WHERE sp.ma = ct.ma_san_pham
				GROUP BY ct.ma_san_pham
				ORDER BY tong_so_luong DESC
				LIMIT 0, 10";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_cung_loai($ma, $ma_loai_san_pham)
	{
		$sql = "select * from san_pham where ma_loai_san_pham = $ma_loai_san_pham and ma != $ma ORDER BY RAND() limit 0, 6";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_theo_san_pham_moi()
	{
		$sql = "select * from san_pham where san_pham_moi = 1 ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from san_pham limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function danh_sach_all()
	{
		$sql = "select * from san_pham ";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function so_luong($ma_loai_san_pham)
	{
		$sql = "select count(*) from san_pham where ma_loai_san_pham = '$ma_loai_san_pham' ";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function xem($ma)
	{
		$ma = intval($ma);

		$sql = "select * from san_pham where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from san_pham where ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function them($ten, $ma_loai_san_pham, $mo_ta_tom_tat, $mo_ta_chi_tiet, $don_gia, $hinh, $san_pham_moi, $so_lan_xem, $ngay_tao, $trang_thai)
	{
		$ten = addslashes($ten);
		$ma_loai_san_pham = intval($ma_loai_san_pham);
		$mo_ta_tom_tat = addslashes($mo_ta_tom_tat);
		$mo_ta_chi_tiet = addslashes($mo_ta_chi_tiet);
		$don_gia = intval($don_gia);
		$hinh = addslashes($hinh);
		$so_lan_xem = intval($so_lan_xem);

		$sql = "INSERT INTO san_pham(ten,ma_loai_san_pham,mo_ta_tom_tat,mo_ta_chi_tiet,don_gia,hinh,san_pham_moi,so_lan_xem,ngay_tao,trang_thai) VALUES ('$ten','$ma_loai_san_pham','$mo_ta_tom_tat','$mo_ta_chi_tiet','$don_gia','$hinh','$san_pham_moi','$so_lan_xem','$ngay_tao','$trang_thai')";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat($ma, $ten, $ma_loai_san_pham, $mo_ta_tom_tat, $mo_ta_chi_tiet, $don_gia, $hinh, $san_pham_moi, $so_lan_xem, $ngay_tao, $trang_thai)
	{
		$ma = intval($ma);
		$ten = addslashes($ten);
		$ma_loai_san_pham = intval($ma_loai_san_pham);
		$mo_ta_tom_tat = addslashes($mo_ta_tom_tat);
		$mo_ta_chi_tiet = addslashes($mo_ta_chi_tiet);
		$don_gia = intval($don_gia);
		$hinh = addslashes($hinh);
		$so_lan_xem = intval($so_lan_xem);

		$sql ="UPDATE san_pham SET ten= '$ten', ma_loai_san_pham= '$ma_loai_san_pham', mo_ta_tom_tat= '$mo_ta_tom_tat', mo_ta_chi_tiet= '$mo_ta_chi_tiet', don_gia= '$don_gia', hinh= '$hinh', san_pham_moi= '$san_pham_moi', so_lan_xem= '$so_lan_xem', ngay_tao= '$ngay_tao', trang_thai= '$trang_thai' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_trang_thai($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE san_pham SET trang_thai = 1 - trang_thai WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function cap_nhat_san_pham_moi($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE san_pham SET san_pham_moi = 1 - san_pham_moi WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	
	function tim_kiem ($tu_khoa)
	{
		$sql = "select * from san_pham where	$ten like %tu_khoa% or 	$mo_ta_tom_tat like %tu_khoa% or 	$mo_ta_chi_tiet like %tu_khoa% or 	$hinh like %tu_khoa%"; 
		$this->setQuery($sql);
		return $this->loadAllRow();	
	}
 }
?>